window.onload = function () {
    cutByItem(1);
}

function cutByItem(pageNO) {
    axios.get("/mail/cutByItem", {
        params: {
            pageNO,
            // senderId:
            subject: $("subjectTxt").value,
            startDate: $("startDateTxt").value,
            endDate: $("endDateTxt").value
        }
    }).then(resp => {
        var info = resp.data;
        var str = "";
        var pageStr = "";
        info.records.forEach(n => {
            str += `<tr><td>${n.subject}</td><td>${n.date}</td>
                    <td><a   href="javascript:findById(${n.mailId})">查看</a>
                    <a href="javascript:update(${n.mailId})" >删除</a></td>
                    </tr>`;
        })
        $("data").innerHTML = str;

        for (var i = 1; i <= info.pages; i++) {
            pageStr += `<a style="margin-left: 10px" href="javascript:cutByItem(${i})">${i}</a>`;
        }

        $("pageSpan").innerHTML = pageStr;
    });
}


function findById(id) {
    $("dialogDiv").style.visibility = "visible";
    axios.get("/mail/findById", {
        params: {
            id,
        }
    }).then(resp => {
        var mail = resp.data;
        $("subjectSpan").innerHTML = mail.subject;
        $("receiverSpan").innerHTML = mail.receiver;
        $("contentSpan").innerHTML = mail.content;
        $("annexSpan").innerHTML = "<a href='/annex/" + mail.annex + "'>下载</a>";
    });
}

function hideDialog() {
    $("dialogDiv").style.visibility = "hidden";
}

function update(id) {
    axios.get("/mail/update", {
        params: {
            id,
        }
    }).then(resp => {
        var info = resp.data;
        if (info == "updateOK") {
            cutByItem(1);
        }
    })
}

